package com.ruoyi.project.system.geocode.service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.domain.Ztree;
import com.ruoyi.project.system.dept.domain.Dept;
import com.ruoyi.project.system.geocode.domain.Geocode;
import com.ruoyi.project.system.geocode.mapper.GeocodeMapper;
import com.ruoyi.project.system.project.domain.Project;
import com.ruoyi.project.system.project.mapper.ProjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 无
 *
 * @author lijing
 * @email 191237578@qq.com
 * @date 2019-03-11 15:31:30
 */
@Service
public class GeocodeService {

    @Autowired
    private GeocodeMapper geocodeMapper;
    @Autowired
    private ProjectMapper projectMapper;

    public Geocode queryByDepCode(String depCode){
        return geocodeMapper.queryByDepCode(depCode);
    }


    public List<Ztree> selectDeptTree(Geocode geocode)
    {
        List<Geocode> geocodeList = geocodeMapper.selectDeptList();
        List<Ztree> ztrees = initZtree(geocodeList,null);
        return ztrees;
    }

    public List<Ztree> requireDeptTree(Geocode geocode)
    {
        List<Geocode> geocodeList = geocodeMapper.requireDepList();
        List<Ztree> ztrees = initZtree2(geocodeList,null);
        return ztrees;
    }

    public List<Ztree> initZtree(List<Geocode> deptList, List<String> roleDeptList)
    {
        Geocode root = new Geocode();
        root.setCode("0000");
        root.setDesp("全国");
        deptList.add(root);
        List<Ztree> ztrees = new ArrayList<Ztree>();
        boolean isCheck = StringUtils.isNotNull(roleDeptList);
        for (Geocode geocode : deptList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(Long.parseLong(geocode.getCode()));
            if(geocode.getCode().endsWith("0000")){
                ztree.setpId(Long.parseLong("0000"));
            }else if(geocode.getCode().endsWith("00")){
                ztree.setpId(Long.parseLong(geocode.getCode().substring(0,2)+"0000"));
            }else{
                ztree.setpId(Long.parseLong(geocode.getCode().substring(0,4)+"00"));
            }
            ztree.setName(geocode.getDesp().trim());
            ztree.setTitle(geocode.getDesp().trim());
            ztree.setType("0");
            ztrees.add(ztree);
        }
        return ztrees;
    }

    public List<Ztree> initZtree2(List<Geocode> deptList, List<String> roleDeptList)
    {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        boolean isCheck = StringUtils.isNotNull(roleDeptList);
        for (Geocode geocode : deptList)
        {
            Ztree ztree = new Ztree();
            List<Project> projectList =  this.projectMapper.selectByGeocode(geocode.getCode().substring(0,2)+'%');
            if(geocode.getCode().equals("100000")){
                ztree.setpId(Long.parseLong(geocode.getCode()));
                ztree.setType("1");
            }else if(geocode.getCode().endsWith("0000")){
                if(projectList!=null&&projectList.size()>0){
                    this.addBj(projectList,geocode,ztrees);
                }
                ztree.setpId(Long.parseLong("100000"));
                ztree.setType("0");
            }
            ztree.setId(Long.parseLong(geocode.getCode()));
            ztree.setName(geocode.getDesp().trim());
            ztree.setTitle(geocode.getDesp().trim());
            ztrees.add(ztree);
        }
        return ztrees;
    }

    public void addBj(List<Project> projectList,Geocode geocode,List<Ztree> ztrees){
        for (Project project : projectList){
            Ztree ztree = new Ztree();
            ztree.setId(project.getProjectId());
            ztree.setName(project.getProjectName().trim());
            ztree.setTitle(project.getProjectName().trim());
            ztree.setpId(Long.parseLong(geocode.getCode()));
            ztree.setType("1");
            ztrees.add(ztree);
        }
    }

}
